package com.pb.infra.oth.util;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.util.Locale;
import java.util.Map;

/**
 * jack处理json相关的工具类
 *
 * @Author:jinyang
 * @Date: 2019/8/6
 * @Time: 19:20
 */
@Slf4j
public class DataBaseConnectionUtils {
    public static Connection connection(Map<String, Object> map){
        if (StringUtil.isEmpty(map.get("databaseType"))) {
            return null;
        }
        if (StringUtil.isEmpty(map.get("dataBaseUser"))) {
            return null;
        }
        if (StringUtil.isEmpty(map.get("dataBasePwd"))) {
            return null;
        }
        if (StringUtil.isEmpty(map.get("cnName"))) {
            return null;
        }
        if (StringUtil.isEmpty(map.get("ip"))) {
            return null;
        }
        if (StringUtil.isEmpty(map.get("port"))) {
            return null;
        }
        Base64 base = new Base64();
        String dbtype = map.get("databaseType").toString();
        String ip = map.get("ip").toString();
        String port = map.get("port").toString();
        String dbname = map.get("cnName").toString();
        String username = map.get("dataBaseUser").toString();
        String password = base.getFromBase64(map.get("dataBasePwd").toString());

        Connection conn = null;
        try {
            String jdbcurl = "";
            if ("ORACLE".equals(dbtype.toUpperCase(Locale.ENGLISH))) {
                jdbcurl = "jdbc:oracle:thin:@" + ip + ":"
                        + port + "/" + dbname;
                conn = DBHelper.initOracle(jdbcurl,username,password, false);
            } else if ("MYSQL".equals(dbtype.toUpperCase(Locale.ENGLISH))) {
                jdbcurl = "jdbc:mysql://" + ip + ":"
                        + port + "/" + dbname
                        + "?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC";
                conn = DBHelper.initMysql(jdbcurl, username,password, false);

            } else if ("DB2".equals(dbtype.toUpperCase(Locale.ENGLISH))) {
                jdbcurl = "jdbc:db2://" + ip + ":"
                        + port + "/" + dbname;
                conn = DBHelper.initDB2(jdbcurl, username,password, false);

            } else if ("SQLSERVER".equals(dbtype.toUpperCase(Locale.ENGLISH))) {
                jdbcurl = "jdbc:sqlserver://" + ip + ":"
                        + port + ";DatabaseName=" + dbname;
                conn = DBHelper.initSQLServer(jdbcurl, username,password, false);

            }
            /*else if("GREENPLUM".equals(dbtype.toUpperCase())){
                jdbcurl = tns;
                conn = DBHelper.initGreenPlum(jdbcurl, username, password, false);
            }else if("HIVE".equals(dbtype.toUpperCase())){
                jdbcurl = tns;
                conn = DBHelper.initHIVE(jdbcurl, username, password, false);
            }*/
            else {
                return null;
            }
        } catch (Exception e) {
            log.error("数据源连接失败",e);
            return null;
        }
        return conn;
    }

    public static void close(Connection conn){
        if (conn != null) {
            DBHelper.closeDB(conn, null, null);
        } else {
            DBHelper.closeDB(conn, null, null);
        }
    }
}
